package array;

public class ListNode {
    public int val;
    public ListNode next;
    public ListNode() {
    }
    public ListNode(int x) {
        val = x;
        next = null;
    }

    public ListNode(int i, ListNode head) {
        val = i;
        next = head;
    }


    public static ListNode build(int[] list){
        ListNode head = new ListNode();
        ListNode tmp = head;
        for (int i = 0; i < list.length; i++) {
            tmp.next = new ListNode(list[i]);
            tmp = tmp.next;
        }
        return head.next;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append(val);
        ListNode next = this.next;
        while (next!=null){
            sb.append(",").append(next.val);
            next=next.next;
        }
        return sb.toString();
    }
}
